package com.gorkr.cronback.service.impl;

import com.gorkr.cronback.pattern.CronPattern;
import com.gorkr.cronback.pattern.designPattern.factory.ValueMatcherFactory;
import com.gorkr.cronback.pattern.designPattern.observer.CronStringPublisher;
import com.gorkr.cronback.pattern.designPattern.observer.ParseToTenTimesObserver;
import com.gorkr.cronback.pattern.designPattern.observer.SpliteCronStringObserver;
import com.gorkr.cronback.service.CronService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.text.ParseException;
import java.util.*;

@Service
public class CronServiceImpl implements CronService {

    CronStringPublisher cronStringSubject = new CronStringPublisher();
    ParseToTenTimesObserver parseToTenTimesObserver = new ParseToTenTimesObserver(cronStringSubject);
    SpliteCronStringObserver spliteCronStringObserver = new SpliteCronStringObserver(cronStringSubject);

//    @Autowired
//    private CronJobService cronJobService;


    // 第一版里的解析调用 已作废
    @Override
    public Map<String, Object> parseCorn(String cronString) throws ParseException {


        return null;
    }

    @Override
    public List<Date> parseCronV2(String cronString) throws Exception {
        System.out.printf("parseCronV2");
        GregorianCalendar startCalendar = new GregorianCalendar();
//        startCalendar.setTime(new Date());
//        GregorianCalendar startCalendar = new GregorianCalendar(2020, Calendar.FEBRUARY,1,1,1);

        GregorianCalendar endCalendar = new GregorianCalendar(2023, Calendar.FEBRUARY,1,1,1);
        ValueMatcherFactory valueMatcherFactory = new ValueMatcherFactory();
        CronPattern cron = new CronPattern(cronString,valueMatcherFactory);


        final List<Date> dates = cron.matchedDates(startCalendar.getTimeInMillis(), endCalendar.getTimeInMillis(), 10);
        return dates;
    }

    @Override
    public Map<String, Object> parseCronV3(String cronString) throws Exception {


        return cronStringSubject.setCronString(cronString);

    }


}